package com.example.demo.controller;


import com.example.demo.config.FaceConfig;
import com.example.demo.config.OssConfig;
import com.example.demo.entity.dto.FileUploadDTO;
import com.example.demo.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Map;


/*
 *
 *   @Author:Sjy
 *   @Date: 2024/5/8-05-08-19:10
 *   文件相关上传或下载
 *
 */
@Api(tags = "文件上传或下载")
@RestController
@RequestMapping("/file")
public class FilesController {

    @Autowired
    private OssConfig ossConfig;
    @Autowired
    private FaceConfig faceConfig;


    //人脸动漫画
    @ApiOperation(value = "人脸动漫画")
    @PostMapping("/upload/faceToAnimated")
    public Result<Map> faceToAnimated(
            @RequestParam("face") MultipartFile face,
            @RequestParam("文件类型")String fileType) throws Exception {
        //数据库保存里面
        String facePath = ossConfig.putObject(face, fileType);
        facePath = "https://"+ossConfig.getBucketName()+"."+ossConfig.getEndpoint()+"/"+facePath;
        //合成人脸
        Map map = faceConfig.faceToAnimated(facePath);
        return Result.success(map);
    }


    //人脸融合
    @ApiOperation(value = "人脸融合接口")
    @PostMapping("/upload/face")
    public Result<Map> uploadFile(
            @RequestParam("face") MultipartFile face,
            @RequestParam("template") MultipartFile template,
            @RequestParam("文件类型")String fileType) throws Exception {
        //数据库保存里面
        String facePath = ossConfig.putObject(face, fileType);
        facePath = "https://"+ossConfig.getBucketName()+"."+ossConfig.getEndpoint()+"/"+facePath;
        //数据库保存里面
        String templatePath = ossConfig.putObject(template, fileType);
        templatePath = "https://"+ossConfig.getBucketName()+"."+ossConfig.getEndpoint()+"/"+templatePath;
        //合成人脸
        Map map = faceConfig.faceMerge(facePath, templatePath);
        return Result.success(map);
    }



    //上传文件接口
    @ApiOperation(value = "通用上传文件的接口")
    @PostMapping("/upload")
    public Result<FileUploadDTO> uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("文件类型")String fileType) throws IOException {
        //数据库保存里面
        String path = ossConfig.putObject(file, fileType);
        //文件回显路径
        String url = "https://"+ossConfig.getBucketName()+"."+ossConfig.getEndpoint()+"/"+path;
        return Result.success(new FileUploadDTO(url,path));
    }


    //下载文件接口
    @ApiOperation(value = "通用下载文件的接口")
    @PostMapping("/download")
    public ResponseEntity<ByteArrayResource> downloadFile(@RequestParam("objectName")String objectName) throws IOException {
        ByteArrayOutputStream outputStream = ossConfig.getObject(objectName);
        //定义一个相应头
        HttpHeaders httpHeaders = new HttpHeaders();

        //获取文件后缀
        String suffix = objectName.substring(objectName.lastIndexOf("."));
        //获取时间戳
        String time = String.valueOf(System.currentTimeMillis());
        objectName = "yy下载站_"+time+suffix;
        System.out.println("objectName = " + objectName);
        //设置下载文件名
        //中文名不乱码
        httpHeaders.setContentDispositionFormData("yy下载站_"+time,URLEncoder.encode(objectName, "utf-8"));
        httpHeaders.add("Cache-Control", "no-cache, no-store, must-revalidate");
        httpHeaders.add("Pragma", "no-cache");
        httpHeaders.add("Expires", "0");

        //设置相应内容是二进制流
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);

        ByteArrayResource byteArrayResource = new ByteArrayResource(outputStream.toByteArray());

        return ResponseEntity.ok()
                .headers(httpHeaders)
                .contentLength(byteArrayResource.contentLength())
                .body(byteArrayResource);
        // 1.9+ 不用写 while循环  .body(new ByteArrayOutputStream(buffer));
    }


}
